package co.fitstart.market.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.entity.order.Order;
import co.fitstart.entity.order.OrderAttribute;
import co.fitstart.entity.order.PaymentStatus;
import co.fitstart.entity.site.Site;
import co.fitstart.market.service.OrderServie;

/**
 * 
 * @author mohanlan
 *
 */
@Service("orderService")
public class OrderServiceImpl implements OrderServie {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.market.service.OrderServie#listUnPostOrder(co.fitstart.entity.site.Site)
     */
    @SuppressWarnings("unchecked")
    public List<Order> listUnPostOrder(Date now, Date yesterday, Site site) {
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("site", site);
        parameters.put("yesterday", yesterday);
        parameters.put("now", now);
        parameters.put("paymentStatus", PaymentStatus.PAID);
        return genericDaoSupport.searchForList("FROM Order WHERE disabled = false AND uploadWxc = false AND site = :site AND paymentStatus = :paymentStatus AND createTime BETWEEN :yesterday AND :now AND payTime IS NOT NULL", parameters);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.market.service.OrderServie#uploadedWxc(co.fitstart.entity.order.Order)
     */
    public void uploadedWxc(Order order) {
        
        // first set order upload as true
        order.setUploadWxc(true);
        genericDaoSupport.update(order);
        
        // then add attr upload wxc time
        OrderAttribute orderAttribute = new OrderAttribute(order, "UPLOAD_WXC_TIME", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        genericDaoSupport.save(orderAttribute);
    }
}
